﻿using System;
using System.Collections.Generic;
using Ue.Infrastructure.DomainBase;
using Ue.Infrastructure.RepositoryFramework;
using Ue.Qcdz.Domain.TreeViews;

namespace Ue.Qcdz.Domain.Articles
{
    public class ArticleService
    {
        private static IArticleRepository repository;


        private static TreeViews.TreeView tagTreeView;

        static ArticleService()
        {
            ArticleService.repository = RepositoryFactory.GetRepository<IArticleRepository, Article>();

            tagTreeView = TreeViewService.Instance.RegisterTreeView(new TreeViews.TreeView(new Guid("749ef038-2b03-4560-94ab-20ded5f591a5"))
            {
                Name = "文章分类树",
                Description = "文章分类的树型结构"
            });

        }

        public static TreeViews.TreeView TagTreeView
        {
            get { return ArticleService.tagTreeView; }
        }

        public static Article GetArticle(object articleKey)
        {
            return ArticleService.repository.FindBy(articleKey);
        }

        public static IList<Article> GetArticles()
        {
            return ArticleService.repository.FindAll();
        }
        public static IDataResult<Article> GetArticles(IArticleDataQuery dataQuery)
        {
            return ArticleService.repository.FindBy(dataQuery);
        }

        public static void AddArticle(Article article)
        {
            ArticleService.repository.Add(article);
        }

        public static void RemoveArticle(Article article)
        {
            ArticleService.repository.Remove(article);
        }
        public static void RemoveArticle(object articleKey)
        {
            Article article = ArticleService.repository.FindBy(articleKey);
            if (article == null)
            {
                throw new ArgumentOutOfRangeException("articleKey");
            }

            ArticleService.repository.Remove(article);
        }

        public static void UpdateArticle(Article article)
        {
            ArticleService.repository[article.Key] = article;
        }
        /// <summary>
        /// 按标题 匹配 的 第一条文章（主要用于判断是否已经存在）
        /// </summary>
        /// <param name="title"></param>
        /// <returns></returns>
        public static Article GetArticle(string title)
        {
            return ArticleService.repository.GetArticle(title);
        }
    }
}
